import { CircleLove, SaveSelect, Select } from "../myTypes"

// 获取图片宽高
export function getImageWidth(url: string) {
  //  图片的宽高
  return new Promise<number>((resolve) => {
    let img = new Image()
    img.src = url
    img.addEventListener('load', () => {
      const height = img.height
      const width = img.width

      resolve((height * 170) / width)
    })
  })
}

// // 获取视频的宽高
function getVideoWidth(fileUrl: string) {
  return new Promise<number>((resolve) => {
    const videoObj = document.createElement('video')
    videoObj.src = fileUrl
    videoObj.onloadedmetadata = function () {
      let height = videoObj.videoHeight //获取视频的高
      let width = videoObj.videoWidth
      resolve((height * 170) / width)
    }
  })
}

function createObj(item: Select) {
  return {
    id: item.id,
    title: item.title,
    img: item.headFigure,
    content: item.content,
    type: item.type,
    storeId: item.storeId,
    isVideo: item.containVideo,
    circleUser: {
      head: item.circleUser.head,
      id: item.circleUser.id,
      name: item.circleUser.name,
      storeId: item.circleUser.storeId
    },
    address: item.address,
    merchantId: item.merchantId
  }
}
// 首页猜你喜欢和热门去处
export function computedHotHeight(list: Array<SaveSelect>) {
  let leftH = 0
  let rightH = 0
  let count = 0
  let leftData: Array<SaveSelect> = []
  let rightData: Array<SaveSelect> = []
  list.forEach(async (item) => {
    // 获取视频高度
    if (/\.mp4/.test(item.img)) {
      const height = await getVideoWidth(item.img)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
       
      }
    } else {
      // 获取图片高度
      const height = await getImageWidth(item.img)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
        
      }
    }
  })
}

// 首页我的关注
export function computedAttentionHeight(list: Array<SaveSelect>,setLeftSelect:React.Dispatch<React.SetStateAction<SaveSelect[]>>,setRightSelect:React.Dispatch<React.SetStateAction<SaveSelect[]>>) {
  let leftH = 0
  let rightH = 0
  let count = 0
  let leftData: Array<SaveSelect> = []
  let rightData: Array<SaveSelect> = []
  list.forEach(async (item) => {
    // 获取视频高度
    if (/\.mp4/.test(item.img)) {
      const height = await getVideoWidth(item.img)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    } else {
      // 获取图片高度
      const height = await getImageWidth(item.img)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    }
  })
}

export default function computedHeight(list: Array<Select>,setLeftSelect:React.Dispatch<React.SetStateAction<SaveSelect[]>>,setRightSelect:React.Dispatch<React.SetStateAction<SaveSelect[]>>) {
  let leftH = 0
  let rightH = 0
  let count = 0
  let leftData: Array<SaveSelect> = []
  let rightData: Array<SaveSelect> = []
  list.forEach(async (item) => {
    // 获取视频高度
    if (/\.mp4/.test(item.headFigure)) {
      const height = await getVideoWidth(item.headFigure)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(createObj(item))
      } else {
        rightH += height
        rightData.push(createObj(item))
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    } else {
      // 获取图片高度
      const height = await getImageWidth(item.headFigure)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(createObj(item))
      } else {
        rightH += height
        rightData.push(createObj(item))
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    }
  })
}

// 计算喜欢数据的高度
export function computedLoveHeight(list: Array<CircleLove>,setLeftSelect:React.Dispatch<React.SetStateAction<CircleLove[]>>,setRightSelect:React.Dispatch<React.SetStateAction<CircleLove[]>>) {
  let leftH = 0
  let rightH = 0
  let count = 0
  let leftData: Array<CircleLove> = []
  let rightData: Array<CircleLove> = []
  list.forEach(async (item) => {
    // 获取视频高度
    if (/\.mp4/.test(item.headfigure)) {
      const height = await getVideoWidth(item.headfigure)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    } else {
      // 获取图片高度
      const height = await getImageWidth(item.headfigure)
      count++
      if (leftH <= rightH) {
        leftH += height
        leftData.push(item)
      } else {
        rightH += height
        rightData.push(item)
      }
      if (count === list.length) {
        setLeftSelect((preLeftData) => [...preLeftData, ...leftData])
        setRightSelect((preRightData) => [...preRightData, ...rightData])
      }
    }
  })
}